package cn.edu.zzuli.admin.service.impl;



import cn.edu.zzuli.admin.listener.IdentityVoListener;
import cn.edu.zzuli.admin.mapper.IdentityMapper;
import cn.edu.zzuli.admin.service.IdentityService;
import cn.edu.zzuli.model.entity.Identity;
import cn.edu.zzuli.model.vo.identity.IdentityQueryVo;
import cn.edu.zzuli.model.vo.identity.IdentityVo;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class IdentityServiceImpl extends ServiceImpl<IdentityMapper, Identity> implements IdentityService {


    @Override
    public Page<Identity> findAllByCond(Integer page, Integer limit, IdentityQueryVo identityQueryVo) {
        //分页查询
        Page<Identity> pageCod = new Page<>(page,limit);
        //条件查询
        QueryWrapper<Identity> wrapper = new QueryWrapper<>();
        String college = identityQueryVo.getCollege();
        String name = identityQueryVo.getName();

        if (!StringUtils.isEmpty(college)){
            wrapper.eq("college",college);
        }
        if (!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        Page<Identity> identityPage = baseMapper.selectPage(pageCod, wrapper);
        return identityPage;
    }

    @Override
    @Cacheable(value = "identity",key ="'ide_page_' + #page")
    public Page<Identity> findAllByPage(Integer page, Integer limit) {
        //分页查询
        Page<Identity> pageCod = new Page<>(page,limit);
        QueryWrapper<Identity> wrapper = new QueryWrapper<>();
        Page<Identity> identityPage = baseMapper.selectPage(pageCod, wrapper);
        return identityPage;
    }


    @CacheEvict(value = "identity", allEntries=true)
    @Override
    public void importStudentData(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), IdentityVo.class,new IdentityVoListener(baseMapper)).sheet()
                    .doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void exportIdentityData(HttpServletResponse response) {
        //设置下载信息
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = "identity";
        response.setHeader("Content=disposition","attachment:filename" + fileName + ".xlsx");

        //查询数据
        List<Identity> identities = baseMapper.selectList(null);
        List<IdentityVo> identityVos = new ArrayList<>();
        for (Identity identity : identities) {
            IdentityVo identityVo = new IdentityVo();
            BeanUtils.copyProperties(identity,identityVo);
            identityVos.add(identityVo);
        }
        try {
            EasyExcel.write(response.getOutputStream(),IdentityVo.class).sheet("Identity")
                    .doWrite(identityVos);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<Identity> getDeletedInf(String stuName) {
        return baseMapper.getDeleted(stuName);
    }

    @Override
    public int backOut(Integer id) {
        return baseMapper.backOut(id);
    }


}
